package com.eduhzy.ttw.teacher.mvp.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.ObjectUtils;
import com.blankj.utilcode.util.StringUtils;
import com.eduhzy.ttw.commonsdk.core.Constants;
import com.eduhzy.ttw.commonsdk.core.EventBusTags;
import com.eduhzy.ttw.commonsdk.core.RouterHub;
import com.eduhzy.ttw.commonsdk.entity.ChooseClassData;
import com.eduhzy.ttw.commonres.utils.RxUtil;
import com.eduhzy.ttw.commonsdk.utils.Utils;
import com.eduhzy.ttw.teacher.R;
import com.eduhzy.ttw.teacher.di.component.DaggerCreateClassComponent;
import com.eduhzy.ttw.teacher.mvp.contract.CreateClassContract;
import com.eduhzy.ttw.teacher.mvp.presenter.CreateClassPresenter;
import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.qmuiteam.qmui.widget.dialog.QMUITipDialog;
import com.xw.repo.XEditText;

import org.simple.eventbus.Subscriber;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;

/**
 * 创建班级
 *
 * @author xubo
 */
@Route(path = RouterHub.APP_CREATECLASSACTIVITY)
public class CreateClassActivity extends BaseActivity<CreateClassPresenter> implements CreateClassContract.View {

    @BindView(R.id.class_level_name)
    TextView classLevelName;
    @BindView(R.id.class_name)
    XEditText className;
    private ChooseClassData chooseClassData;
    private QMUITipDialog dialog;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerCreateClassComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_create_class; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

    }

    @Override
    public void showLoading() {
        if (dialog == null) {
            dialog = new QMUITipDialog.Builder(getActivity())
                    .setIconType(QMUITipDialog.Builder.ICON_TYPE_LOADING)
                    .setTipWord("请稍后")
                    .create();
        }
        dialog.show();
    }

    @Override
    public void hideLoading() {
        if (ObjectUtils.isNotEmpty(dialog)) {
            dialog.dismiss();
        }
    }


    @Subscriber(tag = EventBusTags.CREATE_CLASS)
    public void onEvent(Message msg) {
        switch (msg.what) {
            case EventBusTags.CHOOSE_CLASS_COMPLETE:
                chooseClassData = (ChooseClassData) msg.obj;
                classLevelName.setText(chooseClassData.getName());
                break;
            default:
        }
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        RxUtil.showTipDialog(getActivity(), QMUITipDialog.Builder.ICON_TYPE_NOTHING, message);
//        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @OnClick({R.id.class_level_layout, R.id.create_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.class_level_layout:
                Utils.navigationWithTitle(RouterHub.APP_CHOOSECLASSACTIVITY, Constants.CHOOSE_CLASS);
                break;
            case R.id.create_btn:
                KeyboardUtils.hideSoftInput(this);
                String name = className.getText().toString();
                if (StringUtils.isTrimEmpty(name)) {
                    RxUtil.showTipDialog(this, QMUITipDialog.Builder.ICON_TYPE_FAIL, "请输入班级名称！", 1500);
                    return;
                }
                if (chooseClassData == null) {
                    RxUtil.showTipDialog(this, QMUITipDialog.Builder.ICON_TYPE_FAIL, "请选择年级！", 1500);
                    return;
                }
                mPresenter.saveClassAndReturnData(name, chooseClassData.getName(),chooseClassData.getCode());

                break;
            default:
        }
    }

    @Override
    public Context getActivity() {
        return this;
    }
}
